<h2 style='text-align:center'>商品分类管理</h2>



<select id="MainGoodsType_AddType">
	<option value="1">添加一级分类</option>
	<option value="2">添加二级分类</option>
	<option value="3">添加三级分类</option>
</select>

<div>
	<select id="MainGoodsType_TypeList1" style='display:none'>
		<option value="">一级分类名</option>
	</select>
	<select id="MainGoodsType_TypeList2" style='display:none'>
		<option value="">二级分类名</option>
	</select>
</div>
<div>
	<input placeholder='分类名' id='MainGoodsType_AddInpName' />
	<input placeholder='序号' id='MainGoodsType_AddInpOrder'/>
	<button id='MainGoodsType_AddBtn'>添加</button>　
	<button onclick='MainGoodsType_RefreshBtn()'>刷新</button>
</div>


<div id='MainGoodsType_ViewGoodsTypeArea'>
	<!--div style='display:flex'>
		<div style='flex:1'>
			<span>男装</span>　
			<span style='font-size:12px'>(2)</span>
		</div>
		<div style='flex:1'><button>删除</button></div>
	</div>
	<div style='margin-left:50px;display:flex'>
		<div style='flex:1'>
			<span>男装</span>　
			<span style='font-size:12px'>(2)</span>
		</div>
		<div style='flex:1;text-align:center'><button>删除</button></div>
	</div-->

</div>



<script>
	var s1 = new Array();	//临时装载根级分类对象
	var s2 = new Array();	//临时装载二级分类对象
	var s3 = new Array();	//临时装载三级分类对象
	var type_level = null;
	var Province_id = null;
	var City_id = null;
	var County_id = null;


	//刷新
	function MainGoodsType_RefreshBtn(){
		if(!confirm('确认操作？')){
			return;
		}
		Ajax2({type:'ShareGoodsTypeFlush'},function(res){
			A('刷新成功！');
			GetOrViewShareGoodsType(1);
		},after_url);
	}

	function TypeAddCon(){
		//初始化选择级别
		var a = $('#MainGoodsType_AddType').find('option:selected').val();	//分类级别选择值
		if(!type_level){
			type_level = a;
		}
		$('#MainGoodsType_AddType').change(function(){
			var a = $('#MainGoodsType_AddType').find('option:selected').val();	//分类级别选择值
			type_level = a;
			MainGoodsType_ViewSelTo12();	//根据选择的分类级别，相应显示一二级的选择框

		});
		MainGoodsType_ViewSelTo12();	//根据选择的分类级别，相应显示一二级的选择框

		//点击添加按钮
		$('#MainGoodsType_AddBtn').click(function(){
			var name = ChangeChar($('#MainGoodsType_AddInpName').val().trim());
			var order = ChangeChar($('#MainGoodsType_AddInpOrder').val().trim());

			//Field_Check(/^.+$/,name,'js类名非法！');
			//Field_Check(/^[0-9]{1,3}$/,order,'js序号非法！');

			switch(type_level){
				case 1:
				case '1':
					var level = 1;
					var parent_id = 0;
					break;
				case 2:
				case '2':
					var level = 2;
					var parent_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
					break;
				case 3:
				case '3':
					var level = 3;
					var parent_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
					break;
			}
			var data = {
				type:'ShareGoodsTypeAdd',
				name:name,
				order:order,
				level:level,
				parent_id:parent_id
			};
			Ajax2(data,function(res){
				switch(res){
					case 'err3':
						A('参数不得为空!');
						break;
					case 'err1':
						A('分类已存在!');
						break;
					case 'err2':
						A('添加失败!');
						break;
					case 'ok':
						//A('添加成功!');
						$('#MainGoodsType_AddInpName').val('');
					   	$('#MainGoodsType_AddInpOrder').val('');
						ResetGoodsTypeList();
						break;
				}
			},after_url);
		});
	}

	//根据选择的分类级别，相应显示一二级的选择框 
	function MainGoodsType_ViewSelTo12(){
		switch(type_level){
			case 1:
			case '1':
				$('#MainGoodsType_TypeList1').hide();
				$('#MainGoodsType_TypeList2').hide();
				break;
			case 2:
			case '2':
				
				MainGoodsType_TypeList1Str();	//获取一级分类的option字串并显示
				$('#MainGoodsType_TypeList1').show();
				$('#MainGoodsType_TypeList2').hide();
				break;
			case 3:
			case '3':
				MainGoodsType_TypeList1Str();	//获取一级分类的option字串并显示

				MainGoodsType_TypeList1Str2();	//获取二级分类的option字串并显示


				$('#MainGoodsType_TypeList1').change(function(){
					City_id = null;
					MainGoodsType_TypeList1Str2();	//获取二级分类的option字串并显示
				});


				$('#MainGoodsType_TypeList1').show();
				$('#MainGoodsType_TypeList2').show();
				break;
		}
	}

	//获取一级分类的option字串并显示
	function MainGoodsType_TypeList1Str(){
		var tmp = '';
		for(var i=0;i<s1.length;i++){
			tmp += "<option value='"+s1[i].id+"''>"+s1[i].name+"</option>";
		}
		$('#MainGoodsType_TypeList1').html(tmp);
		if(!Province_id){
			Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
		}else{
			$('#MainGoodsType_TypeList1').val(Province_id);
		}
		$('#MainGoodsType_TypeList1').change(function(){
			Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
		});
	}
	//获取二级分类的option字串并显示
	function  MainGoodsType_TypeList1Str2(){
		//var Province_id = $('#MainGoodsType_TypeList1').find('option:selected').val();
		var s2_tmp = '';
		for(var i=0;i<s1.length;i++){
			if(Province_id == s1[i].id){
				s2_tmp = s1[i].s2;
				break;
			}
		}
		var s2_tmp_str = '';
		for(var i=0;i<s2_tmp.length;i++){
			s2_tmp_str += "<option value='"+s2_tmp[i].id+"''>"+s2_tmp[i].name+"</option>";
		}
		$('#MainGoodsType_TypeList2').html(s2_tmp_str);
		//C($('#MainGoodsType_TypeList2').find('option:selected').val());
		if(!City_id){
			City_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
		}else{
			$('#MainGoodsType_TypeList2').val(City_id);
		}
		$('#MainGoodsType_TypeList2').change(function(){
			City_id = $('#MainGoodsType_TypeList2').find('option:selected').val();
			//C(City_id);
		});


	}



	GetOrViewShareGoodsType();	//获取并展示商品分类数据

	//获取并展示商品分类数据
	function GetOrViewShareGoodsType(sess_stat){
		$('#MainGoodsType_ViewGoodsTypeArea').html('');
		var sess_name = 'sess_GetShareGoodsType';
		if(sess_stat){
			sessionStorage.sess_GetShareGoodsType = '';
		}
		sca({type:'GetShareGoodsType'},function(res){
			GetOrViewShareGoodsType_Parse(res);
		},after_url,sess_name);

	}	

	function GetOrViewShareGoodsType_Parse(res){
		if(/^\[{"id":.*}]$/.test(res)){
			//拆析数据
			var d = JSON.parse(res);
			for(var i=0;i<d.length;i++){
				if(d[i].level == 1){
					s1.push(d[i]);
				}else if(d[i].level == 2){
					s2.push(d[i]);
				}else{
					s3.push(d[i]);
				}
			}
			for(var i=0;i<s2.length;i++){
				var tmp = new Array();
				for(var j=0;j<s3.length;j++){
					if(s2[i].id == s3[j].parent_id){
						tmp.push(s3[j]);
					}
				}
				s2[i].s3 = tmp;
			}

			for(var i=0;i<s1.length;i++){
				var tmp = new Array();
				for(var j=0;j<s2.length;j++){
					if(s1[i].id == s2[j].parent_id){
						tmp.push(s2[j]);
					}
				}
				s1[i].s2 = tmp;
			}
			//C(s1);
			//填充元素

			var str = '';
			for(var i=0;i<s1.length;i++){
				//一级
				str += "<div style='display:flex;margin:20px auto'>";
				str += "	<div style='flex:1'>";
				str += "		<span>"+s1[i].name+"</span>　";
				str += "		<span style='font-size:12px'>("+s1[i].orders+")</span>";
				str += "	</div>";
				str += "	<div style='flex:2'><input placeholder='改序' size=3 style='display:none' /><button onclick=MainGoodsType_ChangeOrder(this,"+s1[i].id+")>改序</button><button onclick=MainGoodsType_Del1("+s1[i].id+")>删除</button><button onclick=MainGoodsType_SubShow(this)>收起</button></div>";
				str += "</div>";

				//二级
				var s2_tmp = s1[i].s2;
				str += "<div>";
				for(var j=0;j<s2_tmp.length;j++){
					str += "<div style='margin-left:50px;display:flex'>";
					str += "	<div style='flex:1'>";
					str += "		<span>"+s2_tmp[j].name+"</span>　";
					str += "		<span style='font-size:12px'>("+s2_tmp[j].orders+")</span>";
					str += "	</div>";
					str += "	<div style='flex:2;text-align:center'><input placeholder='改序' size=3 style='display:none'/><button onclick=MainGoodsType_ChangeOrder(this,"+s2_tmp[j].id+")>改序</button><button onclick=MainGoodsType_Del1("+s2_tmp[j].id+")>删除</button><button onclick=MainGoodsType_SubShow(this)>收起</button></div>";
					str += "</div>";

					//三级
					var s3_tmp = s2_tmp[j].s3;
					str += "<div>";
					for(var k=0;k<s3_tmp.length;k++){
						str += "<div style='margin-left:100px;display:flex'>";
						str += "	<div style='flex:1'>";
						str += "		<span>"+s3_tmp[k].name+"</span>　";
						str += "		<span style='font-size:12px'>("+s3_tmp[k].orders+")</span>";
						str += "	</div>";
						str += "	<div style='flex:1;text-align:right'><input placeholder='改序' size=3 style='display:none' /><button onclick=MainGoodsType_ChangeOrder(this,"+s3_tmp[k].id+")>改序</button><button onclick=MainGoodsType_Del1("+s3_tmp[k].id+")>删除</button></div>";
						str += "</div>";
					}
					str += "</div>";
				}
				str += "</div>";

			}
			$('#MainGoodsType_ViewGoodsTypeArea').html(str);
		}else{
			$('#MainGoodsType_ViewGoodsTypeArea').html('');
		}

		TypeAddCon();
	}


	//改序
	function MainGoodsType_ChangeOrder(o,id){
		if($(o).prev('input').is(':hidden')){
			$(o).prev('input').show();
		}else{
			var order = $(o).prev('input').val().trim();
			if(!order){
				$(o).prev('input').hide();
				return;
			}
			if(!/^[1-9][0-9]*$/.test(order)){
				A('序号必须是整数！');return;
			}
			var data = {
				type:'GoodsTypeChangeOrder',
				id:id,
				order:order,
			};
			sca(data,function(res){
				if(res == 'ok'){
					ResetGoodsTypeList();
				}
			},after_url);
		}
	}


	//删除根类别
	function MainGoodsType_Del1(id){
		if(!id){
			return;
		}
		CheckControl();	//确认操作
		var data = {
			type:'ShareGoodsTypeDel',
			id:id
		};
		Ajax2(data,function(res){
			if(res == 'ok'){
				ResetGoodsTypeList();
			}
		},after_url);
	}

	function ResetGoodsTypeList(){
		//localStorage.ShareGoodsType = '';	//删除页面缓存
		sessionStorage.sess_GetShareGoodsType = '';
		$('#Main_GoodsType').html('');
		LoadMain_GoodsType();
	}


	//展示子类别列表
	function MainGoodsType_SubShow(o){
		var ele = $(o).parent().parent().next('div');
		if(ele.is(":hidden")){
			ele.show();
			$(o).text('收起');
		}else{
			ele.hide();
			$(o).text('展示');
		}
	}

</script>